package cn.gson.crm.control.clazz;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crm.control.base.BaseServlet;
import cn.gson.crm.model.dao.ClazzDao;
import cn.gson.crm.model.entity.Clazz;

/**
 * Servlet implementation class ClazzServlet
 */
@WebServlet("/clazz")
public class ClazzServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
     private ClazzDao clazz=new ClazzDao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClazzServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method=request.getParameter("method");
		if("insert".equalsIgnoreCase(method)){
			insert(request,response);
		}
		if("selectclazzname".equalsIgnoreCase(method)){
			selectclazzname(request,response);
		}
	}

	private void selectclazzname(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name=request.getParameter("name");
		Clazz c=clazz.getClazzByName(name);
		boolean b=false;
		if(c!=null){
			b=true;
		}else{
			b=false;
		}
		response.setContentType("application/json;charset=utf-8");
    	String j=JSONArray.toJSONString(b);
        //输出结果
        response.getWriter().append(j);	
	}

	private void insert(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String  name=request.getParameter("name");
		String staffname=request.getParameter("staffname");
		Clazz  c=new Clazz();
		c.setClazzName(name);
		c.setClazzTeacherId(Integer.parseInt(staffname));
		clazz.insertClazz(c);
		redirect("group?method=select");		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		get(request, response);
	}

}
